#!/usr/bin/python

import pygame, sys, os
from pygame.locals import *

#initialise screen and background
pygame.init()
screen = pygame.display.set_mode([800, 600])
pygame.display.set_caption('Space Shooters!')
background = pygame.image.load('space_bg.png').convert()
screen.blit(background, (0,0))
pygame.display.update()

#set global variables
isFired = False


#Player Class
class Player(pygame.sprite.Sprite):
    pos=[400,560]
##______________________ ##

    def __init__(self):
        pygame.sprite.Sprite.__init__(self) #initialise the parent class

#______________________##

    def loadImage(self,initialPosition):
        self.image=pygame.image.load("hero.jpg").convert()
        self.imagerect=self.image.get_rect() # get the rectangle of the image
        self.imagerect.topleft=initialPosition # set the top left corner to be equal
                                                                #to the pos value (center of the screen)
#____________________________#        

    def display(self):
        screen.blit(background,(0,0)) #display the background
        screen.blit(self.image,self.imagerect) #display the hero
#_____________________________#
    def moveLeft(self):
       self.pos[0]-=5                                                  # move  5 pixels to the left
       self.imagerect.topleft=(self.pos[0],self.pos[1]) #use topleft as reference
       self.updateScreen()                                        #it is usually done by default but it is clearer for me
       print self.pos[0]                                             #see down for this function
       if self.imagerect.topleft==(0,self.pos[1]):       #if the hero reaches the border of the screen
           self.pos[0]=self.pos[0]+5                           #the last movement is ignored because five pixels are added
      
#_______________________________#  same as above function
    def moveRight(self):                                                
       self.pos[0]+=5
       self.imagerect.topright=(self.pos[0]+32,self.pos[1]) #using the top right corner of the image which is situated 32 pixels after the
       self.updateScreen()                                              #to the right of the left corner
       print self.pos[0]
       print self.imagerect.topright[0]

       if self.imagerect.topright[0]>=800:                       # use the corner as a stopper preventing the image from moving
           self.pos[0]=self.pos[0]-5
#_____________________________# function to update screen
    def updateScreen(self): 

       screen.blit(background,(0,0)) #blit the background on the screen
                                                    #it is called after the coordinates of the ship are updated
                                                    # so the background blits on top of the old ship while a new ship appears at the new coordinates
       screen.blit(self.image,self.imagerect) #blits the new ship
       pygame.display.update()



#Missile class
class Missile(pygame.sprite.Sprite):
#____________________________________________________#
    def __init__(self, color, initial_position):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([5,20])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.topleft = initial_position

#_________________________#        
    def fire(self):
        self.rect.move_ip(0,-5)
        screen.blit(background,(0,0))
        screen.blit(self.image,self.rect)
        pygame.display.update()
        
def main():
    #add missile (this is just for Missile demo atm and will be event dependant in future builds)
    m=Missile([255,0,0], [400,560])
    ship=Player()
    ship.loadImage(ship.pos)
    ship.display()
    pygame.display.update()
    
    #event loop
    while 1:
        for event in pygame.event.get():
            if event.type==QUIT:
                exit()
            if event.type== KEYDOWN:
                if event.key==K_LEFT:
                    ship.moveLeft()
                    
                if event.key==K_RIGHT:
                   ship.moveRight()
            if event.type==KEYUP:          #KEYUP prevents players from holding down space and encourages button bashing
                if event.key==K_SPACE:
                    isFired = True
                    while isFired:
                        m.fire()
                    print 'Fire!!!'   #for testing

if __name__ == '__main__': main()
